<?php
function reg_register($data){
	//registro l'utente
	global $_CONFIG;
	
 // print_r($data);
	$uid = reg_get_unique_id();
  //echo "<br>uid: $uid<br>";
  //echo time()."<br>";   
		$result = mysql_query("
	INSERT INTO ".$_CONFIG['table_users']."
	(username, password, mail, temp, uid, regdate)
	VALUES
	('".$data['username']."',MD5('".$data['password']."'), '".$data['mail']."',
	'1', '".$uid."', '".time()."')")
  or die(mysql_error());
	//echo "Result: $result<br>";
	
	$result2=mysql_query("
	INSERT INTO ssts_support
	(sup_name, sup_email, sup_uid)
	VALUES
	('".$data['username']."', '".$data['mail']."', '".$uid."')") or die(mysql_error());
	//LOCAL TEST
	echo "<a href=\"confirm.php?id=".$uid."\">Conferma</a>";
	if($result){
	    return REG_SUCCESS;
		//return reg_send_confirmation_mail($data['mail'], "test@localhost", $uid);
	}else return REG_FAILED;
}

function reg_adduser($data){
	//registro l'utente
	global $_CONFIG;
	
 // print_r($data);
	$uid = reg_get_unique_id();
  //echo "<br>uid: $uid<br>";
  //echo time()."<br>";   
		$result = mysql_query("
	INSERT INTO ".$_CONFIG['table_users']."
	(username, password, mail, temp, uid, regdate)
	VALUES
	('".$data['username']."',MD5('".$data['password']."'), '".$data['mail']."',
	'0', '".$uid."', '".time()."')")
  or die(mysql_error());
	//echo "Result: $result<br>";
	
	$result2=mysql_query("
	INSERT INTO ssts_support
	(sup_name, sup_email, sup_uid)
	VALUES
	('".$data['username']."', '".$data['mail']."', '".$uid."')") or die(mysql_error());
	if($result){
	    return REG_SUCCESS;
	}else return REG_FAILED;
}

function reg_send_confirmation_mail($to, $from, $uid){
	//send confirmation mail
	$msg = "Before you can login, you must activate your account, please click on the following link"
	.$_CONFIG['site']."/confirm.php?id=".$uid."
	";
	return (mail($to, "Conferma la registrazione", $msg, "From: ".$from)) ? REG_SUCCESS : REG_FAILED;
}

function reg_clean_expired(){
	global $_CONFIG;
	
	$query = mysql_query("
	DELETE FROM ".$_CONFIG['table_users']."
	WHERE (regdate + ".($_CONFIG['regexpire'] * 60 * 60).") <= ".time()." and temp='1'");
}

function reg_get_unique_id(){
	//restituisce un ID univoco per gestire la registrazione
	list($usec, $sec) = explode(' ', microtime());
	mt_srand((float) $sec + ((float) $usec * 100000));
	return md5(uniqid(mt_rand(), true));
}

function reg_check_data(&$data){
	global $_CONFIG;
	
	$errors = array();
	
	foreach($data as $field_name => $value){
		$func = $_CONFIG['check_table'][$field_name];
		if(!is_null($func)){
			$ret = $func($value);
			if($ret !== true)
				$errors[] = array($field_name, $ret);
		}
	}
	
	return count($errors) > 0 ? $errors : true;
}

function reg_confirm($uid){
	global $_CONFIG;
	
	$query = mysql_query("
	UPDATE ".$_CONFIG['table_users']."
	SET temp='0'
	WHERE uid='".$uid."'");
	
	return (mysql_affected_rows () != 0) ? REG_SUCCESS : REG_FAILED;
}

function valid_email($email) {
  // First, we check that there's one @ symbol, and that the lengths are right
  if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {
    // Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
    return false;
  }
  // Split it into sections to make life easier
  $email_array = explode("@", $email);
  $local_array = explode(".", $email_array[0]);
  for ($i = 0; $i < sizeof($local_array); $i++) {
     if (!ereg("^(([A-Za-z0-9!#$%&#038;'*+/=?^_`{|}~-][A-Za-z0-9!#$%&#038;'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {
      return false;
    }
  }  
  if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
    $domain_array = explode(".", $email_array[1]);
    if (sizeof($domain_array) < 2) {
        return false; // Not enough parts to domain
    }
    for ($i = 0; $i < sizeof($domain_array); $i++) {
      if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {
        return false;
      }
    }
  }
  return true;
}
?>